package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.mapper.FoodsMapper;
import com.food.mapper.FoodsUsersMapper;
import com.food.mapper.UsersMapper;
import com.food.pojo.Foods;
import com.food.pojo.FoodsUsers;
import com.food.pojo.Users;
import com.food.service.FoodsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author beixiang
 */
@Service
public class FoodsUserServiceImpl implements FoodsUserService {
    @Autowired
    private FoodsUsersMapper foodsUsersMapper;

    @Autowired
    private FoodsMapper foodsMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Boolean updateIsCollectByFoodId(Integer userId, Integer foodId, Integer isFavorite) {
        return foodsUsersMapper.updateIsCollectByFoodId(userId, foodId, isFavorite);
    }

    @Override
    public int getHistoryRecord(Integer userId, Integer foodId) {
        return foodsUsersMapper.getHistoryRecord(userId, foodId);
    }

    @Override
    public Boolean insertHistoryRecord(Integer userId, Integer foodId) {
        return foodsUsersMapper.insertHistoryRecord(userId, foodId);
    }

    /**
     * 通过userId来查询收藏的美食
     * @param userId
     * @return
     */
    @Override
    public List<FoodsUsers> selectAllCollectRecord(Integer userId) {
        QueryWrapper<FoodsUsers> foodsUsersQueryWrapper = new QueryWrapper<>();
        foodsUsersQueryWrapper
                .eq("user_id", userId)
                .eq("is_collect", 1)
                .orderByDesc("collection_time");
        List<FoodsUsers> foodsUsers = foodsUsersMapper.selectList(foodsUsersQueryWrapper);

        for (FoodsUsers foodsUser : foodsUsers) {
            // 获取foodId
            Integer foodId = foodsUser.getFoodId();
            // 通过foodId查询Foods表
            QueryWrapper<Foods> foodsQueryWrapper = new QueryWrapper<>();
            foodsQueryWrapper.eq("food_id", foodId);
            Foods foods = foodsMapper.selectOne(foodsQueryWrapper);
            foodsUser.setFoods(foods);

            // 通过userId查询用户头像
            QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
            usersQueryWrapper.eq("user_id", userId);
            Users users = usersMapper.selectOne(usersQueryWrapper);
            foodsUser.setUserAvatar(users.getUserAvatar());
            foodsUser.setUserName(users.getUserName());
        }

        return foodsUsers;
    }

    @Override
    public FoodsUsers getIsCollectByFoodIdAndUserId(Integer foodId, Integer userId) {
        QueryWrapper<FoodsUsers> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("is_collect")
                .eq("food_id", foodId)
                .eq("user_id", userId);
        return foodsUsersMapper.selectOne(queryWrapper);
    }

    /**
     * 通过userId查询, 返回该用户下的所有历史记录
     * @param userId
     * @return
     */
    @Override
    public List<FoodsUsers> getHistoryByUserId(Integer userId) {
        QueryWrapper<FoodsUsers> foodsUsersQueryWrapper = new QueryWrapper<>();
        foodsUsersQueryWrapper
                .eq("user_id", userId)
                .orderByDesc("id");

        List<FoodsUsers> foodsUsers = foodsUsersMapper.selectList(foodsUsersQueryWrapper);
        for (FoodsUsers foodsUser : foodsUsers) {
            // 对users表查询用户头像, 用户名称
            QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
            usersQueryWrapper.eq("user_id", userId);
            Users users = usersMapper.selectOne(usersQueryWrapper);
            foodsUser.setUserAvatar(users.getUserAvatar());
            foodsUser.setUserName(users.getUserName());

            // 对foods表查询 美食图片, 美食名称, 美食评分
            QueryWrapper<Foods> foodsQueryWrapper = new QueryWrapper<>();
            foodsQueryWrapper.eq("food_id", foodsUser.getFoodId());
            Foods foods = foodsMapper.selectOne(foodsQueryWrapper);
            foodsUser.setFoods(foods);

        }
        return foodsUsers;
    }
}
